package cn.lgp6.ziyou_util.music.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.net.www.http.HttpClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/music")
public class MusicController {


    @RequestMapping("/")
    public String go(){
        return "/music/music.html";
    }


    @GetMapping("/lyric/{id}")
    @ResponseBody
    public String lyric(@PathVariable Long id){
        System.out.println("请求歌词");
        String s = HttpUtil.get("http://music.163.com/api/song/media?id=" + id);
        Map<String,String> map1 = JSON.parseObject(s, Map.class);
        String[] split = map1.get("lyric").split("\n");
        List<Map<String,String>> list=new ArrayList<>();
        for (String s1 : split) {
            if (!"".equals(s1)){
                String time = s1.substring(s1.indexOf("[") + 1, s1.indexOf("]"));
                Map<String,String> map=new HashMap<>();
                map.put("time",String.valueOf(Integer.parseInt(time.split(":")[0])*60+Double.parseDouble(time.split(":")[1])));
                map.put("lyric",s1.substring(s1.indexOf("]")+1));
                list.add(map);
            }
        }
        return JSON.toJSONString(list);
    }

    @GetMapping("/qq/search/{keyword}")
    @ResponseBody
    public String searchQ(@PathVariable String keyword){
        String s = HttpUtil.get("https://c.y.qq.com/soso/fcgi-bin/client_search_cp?aggr=1&cr=1&flag_qc=0&w=" + keyword);
        return s.substring(10,s.length()-1);
    }
}
